home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / SIEVEB~1.FRM < prev    next >
Text File  |  1997-06-14  |  8KB  |  265 lines

  1. VERSION 5.00
  2. Begin VB.Form FSieveBasExeN 
  3.    Caption         =   "Sieve of Eratosthenes Native Server"
  4.    ClientHeight    =   2415
  5.    ClientLeft      =   3405
  6.    ClientTop       =   1740
  7.    ClientWidth     =   5625
  8.    Icon            =   "SieveBasExeN.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   2415
  12.    ScaleWidth      =   5625
  13.    Begin VB.CheckBox chkAll 
  14.       Caption         =   "Get All"
  15.       Height          =   255
  16.       Left            =   3540
  17.       TabIndex        =   13
  18.       Top             =   2040
  19.       Width           =   828
  20.    End
  21.    Begin VB.CheckBox chkLate 
  22.       Caption         =   "Late Bind"
  23.       Height          =   255
  24.       Left            =   2496
  25.       TabIndex        =   12
  26.       Top             =   2040
  27.       Width           =   975
  28.    End
  29.    Begin VB.ListBox lstOutput 
  30.       Height          =   2010
  31.       Left            =   4440
  32.       TabIndex        =   11
  33.       Top             =   120
  34.       Width           =   972
  35.    End
  36.    Begin VB.CheckBox chkDisplay 
  37.       Caption         =   "Display"
  38.       Height          =   255
  39.       Left            =   1536
  40.       TabIndex        =   10
  41.       Top             =   2040
  42.       Width           =   888
  43.    End
  44.    Begin VB.TextBox txtTime 
  45.       Height          =   372
  46.       Left            =   2880
  47.       TabIndex        =   8
  48.       Top             =   1560
  49.       Width           =   1452
  50.    End
  51.    Begin VB.TextBox txtPrimes 
  52.       Height          =   372
  53.       Left            =   2880
  54.       TabIndex        =   6
  55.       Top             =   1080
  56.       Width           =   1452
  57.    End
  58.    Begin VB.TextBox txtMaxPrime 
  59.       Height          =   372
  60.       Left            =   2880
  61.       TabIndex        =   3
  62.       Text            =   " 5000"
  63.       Top             =   600
  64.       Width           =   1452
  65.    End
  66.    Begin VB.TextBox txtIterate 
  67.       Height          =   372
  68.       Left            =   2880
  69.       TabIndex        =   2
  70.       Text            =   "5"
  71.       Top             =   120
  72.       Width           =   1452
  73.    End
  74.    Begin VB.CommandButton cmdExit 
  75.       Cancel          =   -1  'True
  76.       Caption         =   "E&xit"
  77.       Height          =   495
  78.       Left            =   120
  79.       TabIndex        =   1
  80.       Top             =   720
  81.       Width           =   1215
  82.    End
  83.    Begin VB.CommandButton cmdSieve 
  84.       Caption         =   "&Sieve"
  85.       Default         =   -1  'True
  86.       Height          =   495
  87.       Left            =   120
  88.       TabIndex        =   0
  89.       Top             =   120
  90.       Width           =   1215
  91.    End
  92.    Begin VB.Label lbl 
  93.       Caption         =   "Time:"
  94.       Height          =   375
  95.       Index           =   3
  96.       Left            =   1560
  97.       TabIndex        =   9
  98.       Top             =   1560
  99.       Width           =   1215
  100.    End
  101.    Begin VB.Label lbl 
  102.       Caption         =   "Primes:"
  103.       Height          =   375
  104.       Index           =   2
  105.       Left            =   1560
  106.       TabIndex        =   7
  107.       Top             =   1080
  108.       Width           =   1215
  109.    End
  110.    Begin VB.Label lbl 
  111.       Caption         =   "Maximum Prime:"
  112.       Height          =   375
  113.       Index           =   1
  114.       Left            =   1560
  115.       TabIndex        =   5
  116.       Top             =   600
  117.       Width           =   1215
  118.    End
  119.    Begin VB.Label lbl 
  120.       Caption         =   "Iterations:"
  121.       Height          =   375
  122.       Index           =   0
  123.       Left            =   1560
  124.       TabIndex        =   4
  125.       Top             =   120
  126.       Width           =   1215
  127.    End
  128. End
  129. Attribute VB_Name = "FSieveBasExeN"
  130. Attribute VB_GlobalNameSpace = False
  131. Attribute VB_Creatable = False
  132. Attribute VB_PredeclaredId = True
  133. Attribute VB_Exposed = False
  134. Option Explicit
  135.  
  136. Private Declare Function timeGetTime Lib "winmm" () As Long
  137.  
  138. Private fDisplay As Boolean
  139. Private dx As Long, dxOut As Long
  140.  
  141. Private Sub Form_Load()
  142.     dxOut = lstOutput.Left + Width - ScaleWidth
  143.     dx = Width
  144.     Width = dxOut
  145. End Sub
  146.  
  147. Private Sub chkDisplay_Click()
  148.     Static cLastIter As Integer
  149.     If cLastIter = 0 Then cLastIter = txtIterate.Text
  150.     fDisplay = (chkDisplay.Value = vbChecked)
  151.     If fDisplay Then
  152.         txtIterate.Text = 1
  153.         Width = dx
  154.     Else
  155.         txtIterate.Text = cLastIter
  156.         Width = dxOut
  157.     End If
  158. End Sub
  159.  
  160. Private Sub cmdExit_Click()
  161.     Unload Me
  162. End Sub
  163.  
  164. Private Sub cmdSieve_Click()
  165.     Dim ms As Long, i As Integer, iPrime As Integer, cPrime As Integer
  166.     Dim ai() As Integer, vai As Variant
  167.        
  168.     ' Initialize prime variables
  169.     Dim iMaxPrime As Integer, cIter As Integer, cPrimeMax As Integer
  170.     iMaxPrime = txtMaxPrime.Text
  171.     cIter = txtIterate.Text
  172.     cPrimeMax = txtMaxPrime.Text
  173.     txtTime.Text = ""
  174.     txtPrimes.Text = ""
  175.     txtTime.Refresh
  176.     txtPrimes.Refresh
  177.     
  178.     Dim mpcMouse As MousePointerConstants
  179.     mpcMouse = MousePointer
  180.     MousePointer = vbHourglass
  181.     
  182.     ' Default early binding
  183.     If chkLate = vbUnchecked Then
  184.         ' Basic native EXE version, early bind
  185.         Dim sieveBasExeN As New CSieveBasExeN
  186.         sieveBasExeN.MaxPrime = txtMaxPrime.Text
  187.         ' Get one at a time
  188.         If chkAll = vbUnchecked Then
  189.             ms = timeGetTime()
  190.             For i = 1 To cIter
  191.                 sieveBasExeN.ReInitialize
  192.                 Do
  193.                     iPrime = sieveBasExeN.NextPrime
  194.                     If fDisplay And iPrime Then
  195.                         lstOutput.AddItem iPrime
  196.                         lstOutput.TopIndex = lstOutput.ListCount - 1
  197.                         lstOutput.Refresh
  198.                     End If
  199.                 Loop Until iPrime = 0
  200.             Next
  201.             txtTime.Text = timeGetTime() - ms
  202.             txtPrimes.Text = sieveBasExeN.Primes
  203.         Else
  204.             ' Get all at once
  205.             ms = timeGetTime()
  206.             For i = 1 To cIter
  207.                 ReDim ai(0 To 0)    ' Zero array
  208.                 ReDim ai(0 To cPrimeMax)
  209.                 sieveBasExeN.AllPrimes ai()
  210.                 If fDisplay Then
  211.                     For iPrime = 0 To sieveBasExeN.Primes - 1
  212.                         lstOutput.AddItem ai(iPrime)
  213.                     Next
  214.                 End If
  215.             Next
  216.             txtTime.Text = timeGetTime() - ms
  217.             txtPrimes.Text = sieveBasExeN.Primes
  218.         End If
  219.     Else ' Late bound
  220.         ' Set variable at run time
  221.         Dim sieveLate As Object
  222.         Set sieveLate = New CSieveBasExeN
  223.         sieveLate.MaxPrime = txtMaxPrime.Text
  224.         If chkAll = vbUnchecked Then
  225.             ' Get one at a time
  226.             ms = timeGetTime()
  227.             For i = 1 To cIter
  228.                 sieveLate.ReInitialize
  229.                 Do
  230.                     iPrime = sieveLate.NextPrime
  231.                     If fDisplay And iPrime Then
  232.                         lstOutput.AddItem iPrime
  233.                         lstOutput.TopIndex = lstOutput.ListCount - 1
  234.                         lstOutput.Refresh
  235.                     End If
  236.                 Loop Until iPrime = 0
  237.             Next
  238.             txtTime.Text = timeGetTime() - ms
  239.             txtPrimes.Text = sieveLate.Primes
  240.         Else
  241.             ' Get all at once
  242.             ms = timeGetTime()
  243.             For i = 1 To cIter
  244.                 ReDim ai(0 To 0)    ' Zero array
  245.                 ReDim ai(0 To cPrimeMax)
  246.                 sieveLate.AllPrimes ai()
  247.                 If fDisplay Then
  248.                     For iPrime = 0 To sieveLate.Primes - 1
  249.                         lstOutput.AddItem ai(iPrime)
  250.                     Next
  251.                 End If
  252.             Next
  253.             txtTime.Text = timeGetTime() - ms
  254.             txtPrimes.Text = sieveLate.Primes
  255.         End If
  256.     End If
  257.     MousePointer = mpcMouse
  258. End Sub
  259.  
  260. Sub RefreshControls()
  261.     fDisplay = (chkDisplay.Value = vbChecked)
  262.     If lstOutput.ListCount Then lstOutput.Clear
  263. End Sub
  264.  
  265.